from elasticsearch import Elasticsearch

es = Elasticsearch(hosts=['http://42.194.198.29:9200'],
                   basic_auth=("elastic", "chenhaifeng"))

# 创建索引
index_name = 'my_index_t'


if not es.indices.exists(index=index_name):
    es.indices.create(index=index_name)

# 插入文档
doc = {
    'title': 'Example Document',
    'content': 'This is the content of the document.'
}
es.index(index=index_name, body=doc)

# 搜索文档
query = {
    'query': {
        'match': {
            'content': 'example'
        }
    }
}
result = es.search(index=index_name, body=query)
for hit in result['hits']['hits']:
    print(hit['_source'])

# 删除索引
es.indices.delete(index=index_name)
